const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	$U_UserInfo,
} = require('@utils/storage')
const {
	setFormEdit,
	setBtnEdit,
	randomId,
} = require('./api-utils')
const {
	addModuleHistory,
	addApiHistory,
	delNodeHistory,
	apiDetailModifyHistory,
	nameModifyHistory,
} = require('./api-utils-history')
const {
	loadProjectData,
	loadAPIDetail,
} = require('./api-data-2x')

module.exports = {
	/**
	 * 右键菜单:添加模块
	 * @param {[type]} apiComp [description]
	 */
	addModuleMethod(e) {
		let {
			store,
			treeComp,
			treeMenuComp,
		} = this
		if (!store.isLock)
			return false
		let _currentTreeNode = store._currentTreeNode
		if (!_currentTreeNode || !_currentTreeNode.target)
			return false
		let editProjectData = store.editProjectData
		let moduleArr = treeComp.tree('getRoots')
		let lastModule = moduleArr[moduleArr.length - 1]
		let prevNodeId = lastModule.id
		let moduleId = `module-${randomId()}`
		let apiId = `api-${randomId()}`
		let apiNode = {
			id: apiId,
			text: '新建接口',
			attributes: {
				type: 'api',
				// 模块Id
				parentNodeId: moduleId,
				// 前一个api节点ID
				prevNodeId: '',
				// 后一个api节点ID
				nextNodeId: '',
				firstChildId: '',
			},
		}
		let moduleNode = {
			id: moduleId,
			text: `新建模块`,
			state: 'open',
			attributes: {
				type: 'module',
				parentNodeId: '',
				// 前一个模块ID
				prevNodeId,
				// 后一个模块ID
				nextNodeId: '',
				// 第一个api节点id
				firstChildId: apiId,
			},
		}
		treeComp.tree('append', {
			data: $.extend(true, {}, moduleNode, {
				children: [apiNode]
			}),
		});
		// 添加到tree数据结构中
		editProjectData.tree[moduleId] = apiNode
		editProjectData.tree[apiId] = moduleNode
		let api = {
			id: apiId,
			name: '新建接口',
			intro: "",
			method: "GET",
			path: "",
			contentType: "application/x-www-form-urlencoded",
			requestParameterList: '',
			responseParameterList: '',
		}
		editProjectData.apis[apiId] = api;
		// 选中当前节点，保存上一个节点数据
		loadAPIDetailMethod.call(this, api);
		// addModuleHistory.call(this, module)
	},

	/**
	 * 添加子模块
	 * @param {[type]} e [description]
	 */
	addChildModuleMethod(e) {
		let {
			store,
			treeComp,
			treeMenuComp,
		} = this
		if (!store.isLock)
			return false
		let _currentTreeNode = store._currentTreeNode
		if (!_currentTreeNode || !_currentTreeNode.target)
			return false
		let editProjectData = store.editProjectData
		let parentModuleId = _currentTreeNode.id
		let childrenLeng = _currentTreeNode.children.length
		let lastAPINode = _currentTreeNode.children[childrenLeng - 1]
		let prevNodeId = lastAPINode.id
		let moduleId = `module-${randomId()}`
		let apiId = `api-${randomId()}`
		let apiNode = {
			id: apiId,
			text: '新建接口',
			attributes: {
				type: 'api',
				// 模块Id
				parentNodeId: moduleId,
				// 前一个api节点ID
				prevNodeId: '',
				// 后一个api节点ID
				nextNodeId: '',
				firstChildId: '',
			},
		}
		let moduleNode = {
			id: moduleId,
			text: `新建子模块`,
			state: 'open',
			attributes: {
				type: 'module',
				parentNodeId: parentModuleId,
				// 前一个模块ID
				prevNodeId,
				// 后一个模块ID
				nextNodeId: '',
				// 第一个api节点id
				firstChildId: apiId,
			},
		}
		treeComp.tree('append', {
			data: $.extend(true, {}, moduleNode, {
				children: [apiNode]
			}),
		});
		// 添加到tree数据结构中
		editProjectData.tree[moduleId] = apiNode
		editProjectData.tree[apiId] = moduleNode
		let api = {
			id: apiId,
			name: '新建接口',
			intro: "",
			method: "GET",
			path: "",
			contentType: "application/x-www-form-urlencoded",
			requestParameterList: '',
			responseParameterList: ''
		}
		editProjectData.apis[apiId] = api;
		// 选中当前节点，保存上一个节点数据
		loadAPIDetailMethod.call(this, api);
		// addModuleHistory.call(this, api)
	},

	/**
	 * 右键菜单:添加接口
	 * @param {[type]} apiComp [description]
	 */
	addAPIMethod(e) {
		let {
			store,
			treeComp,
			treeMenuComp,
		} = this
		if (!store.isLock)
			return false
		let _currentTreeNode = store._currentTreeNode
		if (!_currentTreeNode || !_currentTreeNode.target)
			return false
		let editProjectData = store.editProjectData
		let moduleId = _currentTreeNode.id
		let childrenLeng = _currentTreeNode.children.length
		let lastAPINode = _currentTreeNode.children[childrenLeng - 1]
		let prevNodeId = lastAPINode.id
		let apiId = `api-${randomId()}`
		let apiNode = {
			id: apiId,
			text: '新建接口',
			attributes: {
				type: 'api',
				// 模块Id
				parentNodeId: moduleId,
				// 前一个api节点ID
				prevNodeId,
				// 后一个api节点ID
				nextNodeId: '',
				firstChildId: '',
			},
		}
		treeComp.tree('append', {
			parent: _currentTreeNode.target,
			data: [$.extend(true, {}, apiNode)]
		});
		// 添加到tree数据结构中
		editProjectData.tree[moduleId] = apiNode
		let api = {
			id: apiId,
			name: '新建接口',
			intro: "",
			method: "GET",
			path: "",
			contentType: "application/x-www-form-urlencoded",
			requestParameterList: '',
			responseParameterList: ''
		}
		editProjectData.apis[apiId] = api;
		// 选中当前节点，保存上一个节点数据
		loadAPIDetailMethod.call(this, api);
		// addApiHistory.call(this, api)
	},

	/**
	 * 删除模块/接口树节点
	 * @param  {[type]} e [description]
	 * @return {[type]}   [description]
	 */
	delTreeMethod(e) {
		let {
			store,
			treeComp,
		} = this
		if (!store.isLock)
			return false;
		let _currentTreeNode = store._currentTreeNode
		if (!_currentTreeNode || !_currentTreeNode.target)
			return false
		let editProjectData = store.editProjectData
		let selectNode = null
		let nodeId = _currentTreeNode.id
		let attributes = _currentTreeNode.attributes
		if (!attributes || !attributes.type)
			return false
		if (attributes.type == 'api') {

		}

		delNodeHistory.call(this, delModule, 'module')
		delNodeHistory.call(this, delApi, 'api')

		treeComp.tree('remove', _currentTreeNode.target)
		store._currentCheckApiDetail = selectNode
		loadAPIDetail(this)
		return

		let apiId = _currentTreeNode.id
		let moduleId = _currentTreeNode.attributes.moduleId || apiId
		let modules = editProjectData.modules
		let module = modules[moduleId]
		let apis = module.apis || {}
		let api = apis[apiId];
		// api节点删除
		if (api && api.id && api.attributes) {
			let {
				prevNodeId,
				nextNodeId,
				moduleId,
				firstChildId,
			} = api.attributes
			let prevNode = apis[prevNodeId];
			let nextNode = apis[nextNodeId];
			if (prevNode && nextNode) {
				// 删除的节点有前后节点
				selectNode = prevNode
				prevNode.attributes.nextNodeId = nextNodeId
				nextNode.attributes.prevNodeId = prevNodeId
			} else if (!prevNode && nextNode) {
				// 删除的节点没有前节点(第一个)
				selectNode = nextNode
				nextNode.attributes.prevNodeId = '';
				// 变更模块第一个节点id
				module.attributes.firstChildId = nextNodeId
			} else if (!nextNode && prevNode) {
				// 删除节点没有后节点(最后一个)
				selectNode = prevNode
				prevNode.attributes.nextNodeId = ''
			} else {
				// Module中只有一个节点,删除后默认选取上一个module第一个
				let prevModule = modules[modules[moduleId].attributes.prevNodeId];
				selectNode = prevModule
			}
			let delApi = module.apis[apiId]
			delNodeHistory.call(this, delApi, 'api')
			delete module.apis[apiId]
		} else if (module && module.id && module.attributes) {
			// 模块节点删除
			let {
				prevNodeId,
				nextNodeId,
			} = module.attributes
			let prevNode = modules[prevNodeId];
			let nextNode = modules[nextNodeId];
			// 选中上一个模块第一个节点
			selectNode = prevNode.apis[prevNode.attributes.firstChildId]
			prevNode.attributes.nextNodeId = nextNodeId
			if (nextNode) {
				nextNode.attributes.prevNodeId = prevNodeId
			} else {
				prevNode.attributes.nextNodeId = ''
			}
			let delModule = modules[moduleId]
			delNodeHistory.call(this, delModule, 'module')
			delete modules[moduleId]
		}
		treeComp.tree('remove', _currentTreeNode.target)
		store._currentCheckApiDetail = selectNode
		loadAPIDetail(this)
	},

	/**
	 * 右键点击显示树结构模块菜单
	 * 默认模块、默认接口不可以删除
	 * @param  {[type]} e    [description]
	 * @param  {[type]} node [description]
	 * @return {[type]}      [description]
	 */
	showTreeMenu(e, node) {
		e.preventDefault()
		let {
			store,
			treeComp,
			treeMenuComp,
		} = this
		if (!store.isLock)
			return false;
		let item1 = treeMenuComp.menu('findItem', '添加模块')
		let item2 = treeMenuComp.menu('findItem', '添加接口')
		let item3 = treeMenuComp.menu('findItem', '删除')
		let opts = node.children ? 'showItem' : 'hideItem'
		let delOps = /(api|module)-0{8}$/.test(node.id) ? 'hideItem' : 'showItem'
		treeMenuComp.menu(opts, item1.target)
		treeMenuComp.menu(opts, item2.target)
		treeMenuComp.menu(delOps, item3.target)
		if (opts == 'hideItem' && delOps == 'hideItem')
			return false
		treeMenuComp.menu('show', {
			left: e.pageX,
			top: e.pageY,
		})
		store._currentTreeNode = node
		if (!node.children) {
			store._currentCheckApiDetail = node
			loadAPIDetail(this, node)
		}
	},

	// 加载某个api详细信息
	loadAPIDetailMethod(node) {
		if (node.children) {
			let {
				treeComp,
			} = this
			if (node.state == 'open')
				treeComp.tree('collapse', node.target)
			else
				treeComp.tree('expand', node.target)
		}
		loadAPIDetailMethod.call(this, node)
	},

	/**
	 * 模块名修改,同步editProjectData中信息
	 * @param {[type]} node [description]
	 */
	setModuleNameMethod(node) {
		let moduleId = node.attributes && node.attributes.moduleId || node.id
		let modules = this.store.editProjectData.modules
		let module = modules[moduleId]
		let api = module.apis && module.apis[node.id] || false
		if (api && api.id) {
			api.text = node.text;
			this.formName.textbox('setValue', api.text)
			nameModifyHistory.call(this, 'api', node.text, moduleId, node.id, )
		} else if (module && module.id) {
			module.text = node.text;
			nameModifyHistory.call(this, 'module', node.text, moduleId)
		}
		this.store._currentTreeNode = null
		return true;
	},
}

function loadAPIDetailMethod(node) {
	let {
		store,
		reqParamsComp,
		resParamsComp,
		formIntro,
		formMethod,
		formPath,
		formContentType,
		treeMenuComp,
	} = this
	if (node.children)
		return false;
	// console.log('store._currentEditApiDetail', store._currentEditApiDetail);
	// 保存上一个接口详细参数
	if (store.isLock && store._currentEditApiDetail) {
		let apiId = store._currentEditApiDetail.id
		let apiDetail = store.editProjectData.apis[apiId];
		apiDetail.intro = formIntro.textbox('getValue');
		apiDetail.method = formMethod.textbox('getValue');
		apiDetail.path = formPath.textbox('getValue');
		apiDetail.contentType = formContentType.textbox('getValue');
		apiDetail.requestParameterList = reqParamsComp.treegrid('getData');
		apiDetail.responseParameterList = resParamsComp.treegrid('getData');
	}
	store._currentCheckApiDetail = node
	loadAPIDetail(this)
}